// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import SDKManager from "../SDKManager";
import zijieConfig from "../zijieConfig";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ksBannerManager  {

    public static _s_instance: ksBannerManager | null = null;
    public static getInstance(): ksBannerManager {
        if (!this._s_instance) {
            this._s_instance = new ksBannerManager();
            this._s_instance.initBanner();
        }

        return this._s_instance;
    }

    qg: any = null;
    private _bannerAd: any = null;
    private _bannerWidth = 0;
    private _bannerHeight = 0;

    _adTime: number = 30;//广告间隔时间
    //_isShow: boolean = false;

    private initBanner() {
        this.qg = window["ks"] || undefined;
        this.addBanner();

    }

    /**
     * 添加banner组件
     * @returns 
     */
    private addBanner() {
        if (this.qg == undefined)
            return;

        this._bannerWidth = Math.floor(this.qg.getSystemInfoSync().screenWidth - 1);
        this._bannerHeight = (this.qg.getSystemInfoSync().screenWidth / 16) * 2;
        let __windowHeight = this.qg.getSystemInfoSync().windowHeight - 1;
        this._bannerAd = this.qg.createBannerAd({
            posId: zijieConfig._ad_banner,
            adIntervals: 35,
            style: {

            },
        });

        this._bannerAd.onLoad(() => {
            SDKManager.onBannerSuccess();
            console.log("banner success");
        })
        this._bannerAd.onError((err) => {
            console.log("banner fail");
            console.log(err);
        })
        this._bannerAd.onClose(() => {
            console.log("banner close");
            this._bannerAd.destroy();
            this._bannerAd = null;
            this.addBanner();

            //广告时间
            this.ADTime();
        })
    }

    ADTime() {
        if (SDKManager.isAd == false) {
            return;
        }
        setTimeout(() => {
            this.showBannerAD();
        }, this._adTime * 1000);
    }

    /**
     * 隐藏Banner广告
     * @returns 
     */
    public hideBannerAd() {
        if (this.qg == undefined)
            return;

        this._bannerAd.hide();
        console.log("banner hide");
    }
    /**
     * 显示Banner广告
     * @returns 
     */
    public showBannerAD() {
        if (this.qg == undefined)
            return;

        this._bannerAd.show();
        console.log("banner show");
    }

    /**
    * 获取Banner高度
    */
    public getBannerHeight(): number {
        if (this.qg == undefined)
            return 100;

        return this._bannerHeight;
    }
}
